<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const obj = {
            id:1,
            age:20,
            name:'张三',
            mag:{
                uage:18
            }
        }

// 1,浅拷贝：for...in... 中，k为属性名;obj[k]为属性值
        const o = {}
        for(let k in obj){
            // console.log(k); 
            // console.log(obj[k]);
            o[k] = obj[k]
        }
        console.log(o);
        // 浅拷贝语法糖：Object.assign(拷贝后的对象，被拷贝的对象)
        Object.assign(o,obj)
        console.log(o);
// 2,深拷贝：封装函数
        function deepCopy(newobj,oldobj){
            for(let k in oldobj){
                let item = oldobj[k]
                // 判断是否是数组
                if(item instanceof Array){
                    newobj[k] = []
                    deepCopy(newobj[k],item)
                }else if(item instanceof Object){
                    // 判断是否是对象
                    newobj[k] = {}
                    deepCopy(newobj[k],item)
                }else{
                    // 属于简单数据类型
                    newobj[k] = item
                }
            }
        }
        deepCopy(o,obj)
        console.log(o);
        o.mag.uage = 9999
        console.log(obj);
    </script>
</body>
</html>